<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-select v-model="listQuery.companyId" placeholder="企业名称" clearable class="filter-item">
        <el-option
          v-for="item in enterpriseList"
          :key="item.companyId"
          :label="item.companyName"
          :value="item.companyId"
        />
      </el-select>-->
      <el-input
        v-model="listQuery.companyName"
        placeholder="企业名称"
        clearable
        class="filter-item"
        style="width: 150px;"
      ></el-input>
      <el-select
        v-model="listQuery.natureId"
        placeholder="企业性质"
        clearable
        class="filter-item"
        style="width: 130px"
      >
        <el-option
          v-for="item in companyNatureList"
          :key="item.id"
          :label="item.dictValue"
          :value="item.id"
        />
      </el-select>
      <el-select
        v-model="listQuery.typeId"
        placeholder="企业类型"
        clearable
        class="filter-item"
        style="width: 130px"
      >
        <el-option
          v-for="item in companyTypeList"
          :key="item.id"
          :label="item.dictValue"
          :value="item.id"
        />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%"
      height="400"
    >
      <el-table-column align="center" label="编号" prop="companyId" min-width="60"></el-table-column>
      <el-table-column align="center" label="企业名称" prop="companyName" min-width="150"></el-table-column>
      <el-table-column align="center" label="企业类型" min-width="100">
        <template slot-scope="{row}">{{row.companyType|typeFilter}}</template>
      </el-table-column>
      <el-table-column align="center" label="企业性质" min-width="100">
        <template slot-scope="{row}">{{row.companyNature|natureFilter}}</template>
      </el-table-column>
      <el-table-column align="center" label="企业安全负责人" prop="safetyOfficerName" min-width="100"></el-table-column>
      <el-table-column align="center" label="联系电话" prop="safetyOfficerTelPhone" min-width="100"></el-table-column>

      <el-table-column align="center" label="操作" fixed="right" min-width="80">
        <template slot-scope="scope">
          <router-link
            :to="'/enterprise/edit/'+scope.row.companyId"
            v-if="checkPermission2('/enterprise/edit/:id')"
          >
            <el-button type="primary" size="small" icon="el-icon-edit">编辑</el-button>
          </router-link>
          <router-link
            :to="'/enterprise/detail/'+scope.row.companyId"
            v-if="checkPermission2('/enterprise/detail/:id')"
          >
            <el-button size="small" icon="el-icon-view">查看</el-button>
          </router-link>
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="delEnterprise(scope.row.companyId)"
            v-if="checkPermission2('/enterprise/delete')"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      class="pageNum"
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageIndex"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { checkPermission2 } from '@/utils/permission'
import { fetchList as fetchEnterpriseList } from '@/api/enterprise'
import { fetchList, deleteEnterprise } from '@/api/enterprise'
import { getBaseDicInfo } from '@/api/baseApi'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
let that
export default {
  data() {
    return {
      companyTypeList: [],
      companyNatureList: [],
      // enterpriseList: [],
      loading: '',
      listQuery: {
        pageIndex: 1,
        limit: 10,
        typeId: '',
        natureId: '',
        companyName: ''
      },
      total: 0, //页码
      tableData: []
    }
  },
  beforeCreate: function() {
    that = this
  },
  created() {
    this.getList()
    this.getCompanyTypeList()
    this.getCompanyNatureList()
    // this.getEnterpriseList()
  },
  components: {
    Pagination
  },
  filters: {
    typeFilter(typeId) {
      let result = ''
      that.companyTypeList.map((type) => {
        if (type.id == typeId) {
          result = type.dictValue
        }
      })
      return result
    },
    natureFilter(natureId) {
      let result = ''

      that.companyNatureList.map((nature) => {
        if (nature.id == natureId) {
          result = nature.dictValue
        }
      })
      return result
    }
  },
  methods: {
    checkPermission2,
    // getEnterpriseList() {
    //   fetchEnterpriseList({ pageIndex: 1, limit: 10 }).then((response) => {
    //     // console.log(response.data)
    //     this.enterpriseList = response.data.records
    //   })
    // },
    getCompanyTypeList() {
      //获取企业类型
      getBaseDicInfo({ typeId: 12 }).then((response) => {
        this.companyTypeList = response.data
      })
    },
    getCompanyNatureList() {
      //获取企业性质
      getBaseDicInfo({ typeId: 11 }).then((response) => {
        this.companyNatureList = response.data
      })
    },
    getList() {
      fetchList(this.listQuery).then((response) => {
        this.tableData = response.data.records
        this.total = response.data.total
        // console.log(this.tableData)
      })
    },
    handleFilter() {
      this.getList()
    },
    delEnterprise(enterpriseId) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          //发请求删除用户
          deleteEnterprise(enterpriseId).then((res) => {
            if (res.data) {
              //删除成功，重新加载数据
              this.getList()
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
            } else {
              this.$message({
                type: 'success',
                message: '删除失败!'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/styles/mixin.scss';

.pageNum {
  margin: 0;
}

.pageBtn {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.splitLine {
  margin-top: 10px;
  margin-bottom: 20px;
  height: 2px;
  background: #000;
  box-shadow: 5px #000 #eee;
}

.clear {
  padding: 0;
  margin: 0;
}

.input {
  flex: 1;
  width: 100%;
  height: 40px;
}

.btnBox {
  display: flex;
  justify-content: flex-end;
}

.line {
  height: 62px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.form {
  width: 100%;
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
}
</style>
